package com.bovink.camera;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

/**
 * com.bovink.camera
 *
 * @author bovink
 * @since 2017/9/10
 */

public class MyDialogFragment extends DialogFragment {
    private static final String CONTENT_KEY = "content";
    private String alertContent;

    public static MyDialogFragment newInstance(String alertContent) {
        MyDialogFragment dialogFragment = new MyDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString(CONTENT_KEY, alertContent);
        dialogFragment.setArguments(bundle);
        return dialogFragment;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getContext());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);// 在低版本Android系统上隐藏标题栏
        dialog.setContentView(createDialogView());
        setCancelable(false);
        return dialog;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            Bundle args = getArguments();
            if (args == null) {
                throw new IllegalArgumentException("Bundle args required");
            }
            alertContent = args.getString(CONTENT_KEY);
        } else {
            alertContent = savedInstanceState.getString(CONTENT_KEY);
        }

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(CONTENT_KEY, alertContent);
    }

    /**
     * 创建对话框视图
     *
     * @return 视图
     */
    private View createDialogView() {
        View root = LayoutInflater.from(getContext()).inflate(R.layout.fragment_request_permission_dialog, null);
        TextView gotoSetting = (TextView) root.findViewById(R.id.tv_goto_setting);
        gotoSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent myAppSettings = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + getContext().getPackageName()));
                myAppSettings.addCategory(Intent.CATEGORY_DEFAULT);
                myAppSettings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(myAppSettings);
                dismiss();
                CameraActivity.gotoSetting = true;
            }
        });

        root.findViewById(R.id.tv_denied).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        TextView alertContentTextView = (TextView) root.findViewById(R.id.tv_content);
        alertContentTextView.setText(alertContent);
        return root;
    }
}
